package com.lxh.lxhcrm.model.service;
import com.lxh.lxhcrm.common.MD5Utils;
import com.lxh.lxhcrm.model.dao.personnel.EmpDao;
import com.lxh.lxhcrm.model.mappers.emp.EmpMapper;
import com.lxh.lxhcrm.model.pojos.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class EmpService {
    @Autowired
    EmpDao empDao;
    @Autowired
    EmpMapper empMapper;

    /**
     * 新增员工，注册
     * @param emp
     */
    public void addEmp(Emp emp){
        emp.setPassword("123456");
        emp.setPortraitSrc("http://122.51.140.72:12345/public/2020/04/29/2d1a84c5-13d0-4cf0-bbd3-ab74b23e2647.png");
        emp.setDefaultPicture("http://122.51.140.72:12345/public/2020/04/29/2d1a84c5-13d0-4cf0-bbd3-ab74b23e2647.png");
        emp.setPassword(MD5Utils.getPwd(emp.getPassword()));
        System.out.println("加密后的"+MD5Utils.getPwd(emp.getPassword()));
        empDao.save(emp);
    }

    /**
     * 登录
     * @param userName
     * @param password
     * @return
     */
    public Emp login(String userName, String password){
        System.out.println(userName+"===="+password);
        System.out.println(userName+"===="+MD5Utils.getPwd(password));
        Emp emp = empMapper.login(userName,MD5Utils.getPwd(password));
        System.out.println(emp);
        return emp;
    }
    /*
    * 折叠面板 根据部门id 查询该部门下的员工信息*/
    public List<Emp> findEmpById(Integer departmentId){
        return empMapper.findEmpById(departmentId);
    }
    /*<!--删除员工  修改is_on_job等于0-->*/
    public void deleteEmp(Integer empId) {
        empMapper.deleteEmp(empId);
    }
    /*<!--根据输入的名字查询员工-->*/
    public List<Emp> findEmpLike(String search){
        return empMapper.findEmpLike(search);
    }
/*个人中心，根据empId查询emp对象*/
    public Emp selectEmpById(Integer empId) {
    return  empMapper.selectEmpById(empId);
}
    /**
     * 查询采购、库存的员工
     * @author：gmx
     * 请不要删我的代码
     */
    public List<Emp> findCaiEmp(){return empMapper.findCaiEmp();}
    public List<Emp> findWareEmp(){return empMapper.findWareEmp();}
/*个人中心修改密码*/
    public void editPwd(Integer empId, String psw) {
        empMapper.editPwd(empId,MD5Utils.getPwd(psw));
    }
    /*个人中心修改头像*/
    public void editImg(Integer empId, String portraitSrc) {
        empMapper.editImg(empId,portraitSrc);
    }
/*个人中心，恢复默认头像*/
    public void restoreImg(Integer empId) {
        empMapper.restoreImg(empId);
    }
/*新增员工时，检查该用户名是否已经被注册*/
    public List<String> checkEmpName(String empName) {
        return empMapper.checkEmpName(empName);
    }
}
